﻿/*
 *  PassBag is multi-platform software that securely manage/store passwords
 *  Copyright (C) <2009>  <Hugo Rodrigues PEREIRA>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 *  You can find a copy of the GPLv3 at project <root directory>/gpl.txt
 *
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace PassBag.Presentation.Controls {

    // password enter event
    public partial class UnlockFileControl : UserControl {

        // delegate declaration
        public delegate void EnterPasswordEventHandler(object sender, PasswordEventArgs e);

        // event declaration
        [DescriptionAttribute("Handle the entered password")]
        [CategoryAttribute("Password Category")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public event EnterPasswordEventHandler PasswordEntered; 

        [DescriptionAttribute("Error message")]
        [CategoryAttribute("Message Category")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string MessageError {
            get { return messageLabel.Text; }
            set { messageLabel.Text = value; }
        }


        public UnlockFileControl() {
            InitializeComponent();

            LoadRessource ();
        }

        #region Load Resource

        /// <summary>
        /// Loads the ressource.
        /// </summary>
        private void LoadRessource () {
            titleLabel.Text = Properties.Resources.UnlockFileControl_TextTitle;
        }

        #endregion


        private void passwordTextBox_KeyPress(object sender, KeyPressEventArgs e) {
            if (e.KeyChar.Equals('\r')) {
                PasswordEventArgs pass = new PasswordEventArgs("Password entered", passwordTextBox.Text);
                PasswordEntered(this, pass);
            }
        }

        public void Clear() {
            messageLabel.Text = string.Empty ;
            passwordTextBox.Clear();
        }
    }

    // custom attributes
    public class PasswordEventArgs : System.EventArgs {

        private string message;
        private string _password;

        public string Password {
            get { return _password; }
        }

        public PasswordEventArgs(string m) {
            this.message = m;
        }

        public PasswordEventArgs(string m, string password) : this(m) {
            this._password = password;
        }

        public string Message() {
            return message;
        }
    } 
}
